
# Creates Figure H1
# Only thing to change below is the working directory

	library(foreign)
	library(readstata13)
	remove(list=objects())

	# Set working directory:
	setwd("C:/Users/Peter/Dropbox/drc_womenempowerment/03_analysis")

	# Data
	data <- read.dta13("DRC2012_ABD_INDIV_v2.dta", convert.factors=F)
	data <- data.frame(data)
	attach(data)


#####################
## PREP DATA
#####################	
	
## time
vars = c("ut000_0to1", "ut001_1to2", "ut002_2to3", "ut003_3to4", "ut004_4to5", "ut005_5to6", "ut006_6to7", "ut007_7to8", "ut008_8to9", "ut009_9to10", "ut010_10to11", "ut011_11to12", "ut012_12to13", "ut013_13to14", "ut014_14to15", "ut15_15to16", "ut16_16to17", "ut17_17to18", "ut18_18to19", "ut19_19to20", "ut20_20to21", "ut21_21to22", "ut22_22to23", "ut23_23to24")

## create separate activities
  ## all
  all <- sapply(vars, function(v) {  
   sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0 & q011_sex==i, na.rm= TRUE)})}
   )
  rownames(all) <- c("w","m")  
  all
  ## organizational, civil and religious activities
  org <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<70 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(org) <- c("w","m")  
  org
  ## money generating activities
  money <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<70 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(money) <- c("w","m")  
  money
  ## leisure and sport
  sport <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<60 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(sport) <- c("w","m")  
  sport
  ## educational activities
  edu <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<50 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(edu) <- c("w","m")  
  edu
  ## personal services
  personal <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<40 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(personal) <- c("w","m")  
  personal
  ## OTHER domestic work (26, 27. 21 and 23 as well but they are (as good as) empty)
  domestic <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<30 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(domestic) <- c("w","m")  
  domestic
  ## CLEANING HOUSE: 25
  cleaning <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<26 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(cleaning) <- c("w","m")  
  cleaning
  ## COLLECT WATER: 24
  water <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<25 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(water) <- c("w","m")  
  water
  ## COOKING: 22 (21 is emptyish)
  cooking <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<23 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(cooking) <- c("w","m")  
  cooking
  ## sleep etc
  sleep <- sapply(vars, function(v) {  
    sapply(0:1, function(i) {sum(!is.na(data[,v]) & data[,v]>0  & data[,v]<20 & q011_sex==i, na.rm= TRUE)})}
  )
  rownames(sleep) <- c("w","m")  
  sleep
  ############################################


  other<-100-all/all*100
  org<-100-org/all*100
  money<-100-money/all*100
  sport<-100-sport/all*100
  edu<-100-edu/all*100
  personal<-100-personal/all*100
  domestic<-100-domestic/all*100
  cleaning<-100-cleaning/all*100
  water<-100-water/all*100
  cooking<-100-cooking/all*100
  sleep<-100-sleep/all*100

#####################
## DRAW FIGURE
#####################	
  
pdf(file="FigH1_timeallocation.pdf", width=10, height=12)
  
par(mfrow=c(2,1))
x <- seq(1,24,1)
yZERO <- rep(0,24)
yHUNDRED <- rep(100,24)
  
#####################
## FOR WOMEN
#####################	

par(mar = c(2, 3, 4, 1)) # b, l, t, r 
  
plot(0,0, xlim=c(1,24), ylim=c(0,100), xlab="", ylab="", cex.lab=1.5, axes=F, frame.plot=F, type="n", , main="Women", cex.main=1.5)
axis(1, at=seq(1,24,1), cex.axis=1)
axis(2, at=seq(0,100,20), cex.axis=1, label=c("0%", "20%", "40%", "60%", "80%", "100%"), las=1)			

# draw polygons    
  polygon(c(x,rev(x)),c(other[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(org[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(money[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(sport[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(edu[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(personal[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(domestic[1,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(cleaning[1,],rev(yHUNDRED)),col="darkgray", border = "gray")
  polygon(c(x,rev(x)),c(water[1,],rev(yHUNDRED)),col="gray", border = "gray")
  polygon(c(x,rev(x)),c(cooking[1,],rev(yHUNDRED)),col="lightgray", border = "gray")
  polygon(c(x,rev(x)),c(sleep[1,],rev(yHUNDRED)),col="white", border = "gray")

# Add text
  text(9, 1, "Other", cex=1.2,adj = c(0,0))
  text(10, 5, "Social", cex=1.2,adj = c(0,0))
  text(8, 20, "Income-generating", cex=1.2,adj = c(0,0))
  text(16, 15, "Leisure", cex=1.2,adj = c(0,0))
  text(21.2, 12, "Education", cex=1.2,adj = c(0,0))
  lines(c(19,21),c(8,13), cex=2, lwd=1.2)
  text(16, 22, "Services", cex=1.2,adj = c(0,0))
  text(9.5, 50, "Field", cex=1.2,adj = c(0,0))
  text(8, 63, "Cleaning", cex=1.2,adj = c(0,0))
  text(8.5, 73, "Water", cex=1.2,adj = c(0,0))
  text(14, 45, "Cooking", cex=1.2,adj = c(0,0))
  text(16, 90, "Sleep and personal care", cex=1.2,adj = c(0,0))
  
#####################
## FOR MEN
#####################	
  
  par(mar = c(2, 3, 4, 1)) # b, l, t, r 
  
  plot(0,0, xlim=c(1,24), ylim=c(0,100), xlab="", ylab="", cex.lab=1.5, axes=F, frame.plot=F, type="n", , main="Men", cex.main=1.5)
  axis(1, at=seq(1,24,1), cex.axis=1)
  axis(2, at=seq(0,100,20), cex.axis=1, label=c("0%", "20%", "40%", "60%", "80%", "100%"), las=1)			
  
  # draw polygons    
  polygon(c(x,rev(x)),c(other[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(org[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(money[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(sport[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(edu[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(personal[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(domestic[2,],rev(yHUNDRED)),col="white", border = "gray")
  polygon(c(x,rev(x)),c(cleaning[2,],rev(yHUNDRED)),col="darkgray", border = "gray")
  polygon(c(x,rev(x)),c(water[2,],rev(yHUNDRED)),col="gray", border = "gray")
  polygon(c(x,rev(x)),c(cooking[2,],rev(yHUNDRED)),col="lightgray", border = "gray")
  polygon(c(x,rev(x)),c(sleep[2,],rev(yHUNDRED)),col="white", border = "gray")
  
  # Add text
  text(9, 1, "Other", cex=1.2,adj = c(0,0))
  text(10, 7, "Social", cex=1.2,adj = c(0,0))
  text(8, 28, "Income-generating", cex=1.2,adj = c(0,0))
  text(16.5, 30, "Leisure", cex=1.2,adj = c(0,0))
  text(20.2, 38.1, "Education", cex=1.2,adj = c(0,0))
  lines(c(17.5,20),c(38.1,38.1), cex=2, lwd=1.2)
  text(16, 45, "Services", cex=1.2,adj = c(0,0))
  text(9.5, 70, "Field", cex=1.2,adj = c(0,0))
  text(19.2, 58, "Cleaning", cex=1.2,adj = c(0,0))
  lines(c(17,19),c(58,59), cex=2, lwd=1.2)
  text(16.2, 65, "Water", cex=1.2,adj = c(0,0))
  lines(c(16.5,16),c(59,66), cex=2, lwd=1.2)
  text(13.5, 54, "Cooking", cex=1.2,adj = c(0,0))
  text(16, 90, "Sleep and personal care", cex=1.2,adj = c(0,0))
  
dev.off()

													## END ##